let parentId = 0
let spayDetailsId = []
$(function () {
    $("#gradeTable").bootstrapTable({
        method: "get",
        contentType: "application/json",
        url: "/inputmanagerController/getspaystoreTag2",  //把库存标记为已调度（2）的查询出来
        // striped: true,
        pageNumber: 1,
        pagination: true,
        sidePagination: 'server',
        pageSize: 5,
        pageList: [1, 3, 5, 7],
        queryParams: function (parmes) {
            return {
                pageNum: (parmes.offset / parmes.limit) + 1,
                pageSize: parmes.limit,
                storer: $("#storer").val(),
                payId: $("#gatherId").val(),
                registerTime: $("#registerTime").val(),
                checker: $("#checker").val()
            }
        },
        columns: [
            { title: "出库单编号", field: 'payId', align: "center" },
            { title: "出库理由", field: 'reason', align: "center" },
            { title: "出库详细理由", field: 'reasonexact', align: "center",
                formatter: function (value,row,index) {
                    if(value==undefined){
                        return "暂无留言"
                    }else {
                        return  value
                    }}},
            { title: "登记时间", field: 'registerTime', align: "center" },
            { title: "总计数", field: 'amountSum', align: "center" },
            { title: "总金额（元）", field: 'costPriceSum', align: "center" },
            {
                title: "登记", field: 'payId', align: "center",
                formatter: function (value,row,index) {
                    console.log(row)
                    var btn1 = "<button class=\"btn btn-success btn-sm\" onclick='checkTag("+value+","+JSON.stringify(row)+")'>登记</button>";
                    return btn1
                }
            }
        ]
    })
    // $("#condition").click(function(){
    //     $("#gradeTable").bootstrapTable("refresh")
    // })
})
//审核  跳页面 把该出库单 和明细显示出来
function checkTag(payId,parse) {
    console.log(parse)
    $("#rkdbh").html(parse.payId)                    //出库单编号
    $("#rkxqly").html(parse.reasonexact)          //出库详情理由
    $("#rkly").html(parse.reason)                   //出库理由
    $("#two").show()
    $("#one").hide()
    $("#rkr").val(parse.storer)
    $("#rkly").val(parse.reason)
    $("#zjs").html(parse.amountSum)
    $("#zje").html(parse.costPriceSum)
    $("#djr").html(parse.register)
    $("#djsj").html(parse.registerTime)
    $("#bz").val(parse.remark)
    parentId=parse.id
    $.ajax({
        url:"/inputmanagerController/byParentIdquerysPayDetailspayTag2",
        type:"get",
        contentType: "application/json",
        async:true,
        data:{
            id:parse.id,
        },
        success:function (list) {
            console.log(list)
            let content="";
            for (let i = 0; i < list.length; i++) {
                spayDetailsId.push(list[i].id)
                parentId=list[0].parentId
                //$(".paidAmount")[i].value=list[i].amount
                content += "<tr>" +
                    "<td>"+ list[i].productId +"</td>" +
                    "<td>" + list[i].productName + "</td>" +
                    "<td>成品库</td>" +
                    "<td>电子-计算机-服务器</td>" +
                    "<td class='yrksl'>" + list[i].amount + "</td>" +
                    "<td>" + 0.0 + "</td>" +//list[i].gatheredAmount
                    "<td><input type='number' class='paidAmount' placeholder='出库数量''></td>" +     //<button class='btn btn-primary' onclick='ByproductIdqueryIsdesignCellTag("+list[i].productId+","+list[i].id+")'
                    "</tr>"
            }
            $("#tbody").append(content)
        }
    })
}
//提交
function pass() {
    let yrksl = $(".yrksl")
    let bcrksl = $(".paidAmount")
    for (let i = 0; i <yrksl.length ; i++) {
        if(bcrksl[i].value==""){
            alert("本次出库数量不能为空")
            return;
        }
        if(parseInt(bcrksl[i].value)<0){
            alert("本次出库数量不能为负数")
            return;
        }
        if(parseInt(yrksl[i].innerHTML)!=parseInt(bcrksl[i].value)){
            alert("本次出库数量与应入库数量不一致，请重新确认")
            return;
        }
    }
    let paidAmountsElement = $(".paidAmount")
    let paidAmounts=[]
    for (let i = 0; i < paidAmountsElement.length; i++) {
        paidAmounts.push(paidAmountsElement[i].value)
    }
    $.ajax({
        url:"/inputmanagerController/BysPayDetailsIdupdatesgatheredAmount", //通过出库明细id 修改该id下的确认出库件数 （gathered_amount）
        type:"get",
        contentType: "application/json",
        async:true,
        data:{
            parentId:parentId,
            id:spayDetailsId.toString(),
            gatheredAmount:paidAmounts.toString()
        },
        success:function (data) {
            console.log(data)
            if(data==1){
                alert("登记成功，请前往复核")
                parent.document.getElementById('iframeId').contentWindow.location.reload(true);
            }
        }
    })
}

function demand() {
    $("#gradeTable").bootstrapTable("refresh")
}

//点击调度  根据产品明细中的产品id  查询档案表该产品id是否  有设计库存分配标准（design_cell_tag） 是否设计  如果  未设计（0）不能调度  设计（1）修改相关信息
function ByproductIdqueryIsdesignCellTag(productId,id) {
    console.log(productId,id)
    $.ajax({
        url:"/inputmanagerController/ByproductIdqueryIsdesignCellTag",
        type:"get",
        contentType: "application/json",
        async:true,
        data:{
            parentId:parentId,
            id:id,
            productId:productId,
        },
        success:function (data) {
            console.log(data)
            if(data=="1"){
                alert("调度成功")
            }else {
                alert("对不起，该产品尚未分配存储空间，请返回")
            }
        }
    })
}
function fh() {
    $("#one").show()
    $("#two").hide()
}